import { defineStore } from "pinia"
import { store } from "@/store"
import { UserState } from "../types/userState"
import { LoginRes } from "@/api/user/types/res"
import { getMenuList } from "@/api/menu"

const defaultUserState = (): UserState => ({
    user: JSON.parse(localStorage.getItem('user')!) ?? {},
})

export const UserStore = defineStore({
    id: "user-store",
    state: defaultUserState,
    getters: {
        token(): string {
            return this.user.token
        },
        tokenHeader(): string {
            return this.user.tokenHeader
        }
    },
    actions: {
        setUserInfo(loginRes: LoginRes) {
            //将用户信息存入本地
            localStorage.setItem("user", JSON.stringify(loginRes));
            //手动更新store的token和tokenHeader
            this.user = loginRes
        }
        ,
        resetState(): void {
            //清除本地用户信息，注意要先清除用户信息再重置state
            localStorage.clear()
            sessionStorage.clear()
            this.$reset()
        }
    }
})


export function useUserStore() {
    return UserStore(store)
}
